/*
 * @Author: 小花
 * @Date: 2022-01-24 02:38:19
 * @LastEditors: 小花
 * @LastEditTime: 2022-05-19 17:10:22
 * @Description: 公共函数
 */
import axios from '@/js/axios'
import { ElMessage } from 'element-plus'

/**
 * @description: 保存用户登录信息到localStorage,并清空socketID
 * @param {*} data json对象
 * @return {*}
 * @author: 小花
 */
export function saveLoginInfo (data) {
  // 深拷贝
  let obj = JSON.parse(JSON.stringify(data));

  localStorage.setItem("satoken", obj.satoken);
  delete obj.satoken
  localStorage.setItem("userInfo", JSON.stringify(obj));
  sessionStorage.removeItem("socketID");
}

export function getUserInfo () {
  return {
    nickname: JSON.parse(localStorage.getItem("userInfo"))['nickname'],
    headimgurl: JSON.parse(localStorage.getItem("userInfo"))['headimgurl'],
    loginType: JSON.parse(localStorage.getItem("userInfo"))['loginType'],
  }
}

/**
 * @description: 清除[userInfo, satoken]
 * @param {*}
 * @return {*}
 * @author: 小花
 */
export function clearLoginInfo () {
  localStorage.removeItem("userInfo");
  localStorage.removeItem("satoken");
}

/**
 * @description: 设置限流sign，并配置限流时间
 * @param {*} sign 标志
 * @param {*} seconds 秒/默认6秒
 * @return {*} void
 * @author: 小花
 * 使用 if (getQps('queryWorkerOrder')) return;
            setQps('queryWorkerOrder');
 */
export function setQps (sign, seconds = 6) {
  let cookie = `${sign} = true; max-age = ${seconds}`;
  document.cookie = cookie;
}

/**
 * @description: 获取限流sign是否存在
 * @param {*} sign 标志
 * @return {*} true:存在，false:不存在
 * @author: 小花
 */
export function getQps (sign) {
  let cookieArr = document.cookie.split(";")
  for (let index in cookieArr) {
    let key = cookieArr[index].split("=")[0]
    if (key == sign) {
      ElMessage.info({
        message: '操作过于频繁，请稍后再试',
        showClose: true
      });
      return true;
    }
    if (index == cookieArr.length - 1) {
      return false;
    }
  }
}

/**
 * @description: 处理后端返回数组字符串图片地址
 * @param {*} imgUrl
 * @return {*}
 * @author: 小花
 */
export function imgUrlAddress (imgUrl) {
  let arrUrl = JSON.parse(imgUrl);
  if (arrUrl != null) {
    arrUrl.forEach((element, index) => {
      arrUrl[index] = axios.defaults.baseURL + element;
    });
  }
  return arrUrl;
}

/**
 * @description: 报修Tag状态样式
 * @param {*} statusCode
 * @return {*}
 * @author: 小花
 */
export function repairStatusStyle (statusCode) {
  // [待受理，已受理，已完结，已驳回，待完结]
  return ['info', '', 'success', 'danger', 'warning'].at(statusCode - 1);
}

export function createOrderDetailsStep (orderProcessList = null) {
  let array = new Array();
  let list = orderProcessList;
  if (orderProcessList != null && orderProcessList.length == undefined) list = [orderProcessList];

  list.forEach((item, index) => {
    let steps = {
      orderId: item.orderId,
      currentStep: 0,
      step: {
        contactConfirmation: { title: "联系确认", description: "", status: "" },
        applyMaterial: { title: "申报材料", description: "", status: "" },
        chargeOn: { title: "收费开启", description: "", status: "" },
        orderCompleted: { title: "订单完结", description: "", status: "" }
      }
    }

    Object.keys(steps.step).forEach((key, idx) => {
      steps.step[key].status = item[key]
      if (item[key] == 'process') {
        steps.currentStep = idx;
      }
      if (key == 'orderCompleted' && item[key] == 'success') {
        steps.currentStep = 4;
      }
    })
    array.push(steps);
  })
  return array;
}